/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fourplay.frontEnd;

import fourplay.ai.Ai;
import fourplay.ai.Node;
import fourplay.constants.Constants;
import java.util.Scanner;

/**
 *
 * @author CJ
 */
public class GameEngine{

    protected Scanner input;
    protected boolean gameOver;
    protected State state;

    protected boolean computerTurn;

    public GameEngine(Scanner input){
        this.input = input;
        this.gameOver = false;
        this.state = new State();
    }

    public void run(){
        this.initMessages();
        while(!gameOver){
            if(!this.computerTurn){
                makePlayerMove();
            }else{
                makeComputerMove();
            }
            char winner = this.state.getWinner();
            if(winner != Constants.EMPTY || winner == Constants.CATS_GAME){
                this.gameOver = true;
                printGameOver(winner);
            }else{
                this.computerTurn = !this.computerTurn;
            }
        }
    }

    protected void printGameOver(char winner){
        printState(state);
        System.out.println("GAME OVER!");
        if(winner == Constants.COMP_SYM)
            System.out.println("Sorry you lost, my ai rocks.");
        else if(winner == Constants.CATS_GAME)
            System.out.println("Nice Job, you held out against the computer.");
        else
            System.out.println("Nice you beat the computer, time to go fix the ai.");
    }

    protected void makePlayerMove(){
        printState(state);
        System.out.println("Please enter the number of the col you would like to place in.");
        boolean valid = false;
        int colNum = -1;
        while(!valid){
            String strInput = this.input.next();
            try{
                colNum = Integer.parseInt(strInput);
                if(colNum <= 0 || colNum > Constants.NUM_COL){
                    throw new IllegalArgumentException("Invalid Column Number");
                }
                else if(this.state.addPlayer(colNum - 1) == -1){
                    throw new IllegalArgumentException("Invalid Column Number(Column is full)");
                }
                else{
                    valid = true;
                }
            }catch(Exception e){
                System.out.println("Please enter a valid Column Number.");
                System.out.println(e);
            }
        }
    }

    protected void makeComputerMove(){
        //Make sure to clone the state to make sure its going to be unmodifyed
       //state.addComputer(Ai.getRandomMove(state.clone()));
        state.addComputer(Ai.getHeristicMove(state.clone(), Constants.depth, Constants.COMP_SYM));

    }

    protected void initMessages(){
        System.out.println("Hello, Welcome to this version of Fourplay.");
        System.out.println("Today you will be playing against a computer.");
        System.out.println("Good Luck and Have Fun.");
        System.out.println("Your symbol is: " + Constants.PLAY_SYM);
        System.out.println("Would you like to move first?(y/n)");
        getTurnOrder();
    }

    protected void getTurnOrder(){
        boolean valid = false;
        char[] charInput = null;
        while(!valid){
            charInput = input.next().toCharArray();
            if(charInput.length == 1 && (charInput[0] == 'y' || charInput[0] == 'n'))
                valid = true;
            else
                System.out.println("Please Enter a Valid Choice.");
        }
        if(charInput[0] == 'y')
            this.computerTurn = false;
        else
            this.computerTurn = true;
    }

    public void printState(State state){
        System.out.print(state);
    }

}
